<?php
namespace App\Http\Questionnaire\Controllers;

use Illuminate\Http\Request;
use App\Http\Admin\Controllers\Controller as Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use PhpOffice\PhpWord\PhpWord;


class PageController  extends Controller
{
    public function __construct(Request $request)
    {
        $token = getToken($request);
        $uid = Redis::get("code:" . $token);
        $this->uid = $uid;
    }
    /**
     * 用户新增问卷
     */
    public function user_add_questionnaire(Request $request,$qid){

        $data = $request->only('qid');
        $data['createdTime']=date("Y-m-d H:i:s",time());
        $data['updateTime']=date("Y-m-d H:i:s",time());
        $data['userId']=$this->uid;
        $data['agreement_number']=time().rand(1000,9999);

        $res = DB::table("Q_q_p_users")->insertGetId($data);
        DB::table("Q_q_p_users")->where("id",$res)->update(["history"=>$res]);

        if($res){
            DB::table("Q_user_answer")->insertGetId([
                "qpid"=>965,
                "quid"=>$res,
                "value"=>"(2019)京x民初x号",
                'fornum'=>0
            ]);

            $questionnaire = DB::table("q_questionnaire")->where("id",$data['qid'])->first();
            if($questionnaire['Ofqp']){
                $Ofqp = json_decode($questionnaire['Ofqp']);

                foreach ($Ofqp as $k=>$v){
                    DB::table("Q_user_answer")->insertGetId(['value'=>1,'qpid'=>$v,'fornum'=>1,'quid'=>$res]);
                }

            }



            return [
                'status_code'      => 200,
                'message'     =>  '提交成功',
                'data' => $res
            ];
        }else{
            return [
                'status_code'      => 200,
                'message'     =>  '提交失败 联系后端开发',
                'data' => $res
            ];
        }

    }

    public function user_add_questionnaire_aladdin(Request $request,$qid){
        $data=$request->only("qid",'aladin_orderId');

        $quData=DB::table("Q_q_p_users")->where('aladin_orderId',$data['aladin_orderId'])->first();
        if($quData){
            $this->tongbu_aladin($data,$quData['id']);
            return [
                'status_code'      => 200,
                'message'     =>  '提交成功',
                'data' => $quData['id']
            ];
        }

        $data['createdTime']=date("Y-m-d H:i:s",time());
        $data['updateTime']=date("Y-m-d H:i:s",time());
        $data['userId']=$this->uid;
        $data['aladin_orderId']=$data['aladin_orderId'];
        $data['agreement_number']=time().rand(1000,9999);

        $res = DB::table("Q_q_p_users")->insertGetId($data);

        $this->tongbu_aladin($data,$res);   

        DB::table("Q_q_p_users")->where("id",$res)->update(["history"=>$res]);
        $this->tongbu_aladin($data,$res);
        if($res){
            DB::table("Q_user_answer")->insertGetId([
                "qpid"=>965, 
                "quid"=>$res,
                "value"=>"(2019)京x民初x号2",
                'fornum'=>0
            ]);

            $questionnaire = DB::table("q_questionnaire")->where("id",$data['qid'])->first();
            if($questionnaire['Ofqp']){
                $Ofqp = json_decode($questionnaire['Ofqp']);

                foreach ($Ofqp as $k=>$v){
                    DB::table("Q_user_answer")->insertGetId(['value'=>1,'qpid'=>$v,'fornum'=>1,'quid'=>$res]);
                }

            }


            return [
                'status_code'      => 200,
                'message'     =>  '提交成功',
                'data' => $res
            ];
        }else{ 
            return [
                'status_code'      => 200,
                'message'     =>  '提交失败 联系后端开发',
                'data' => $res
            ];
        }

    }


    public function tongbu_aladin($data,$quid){

        

        $data=$this->order_flyjs($data['aladin_orderId']);
        $arr=[
            "婚姻信息"=> 16628, 
            "子女信息"=>16629, 
            "房产情况"=>16630, 
            "车辆情况"=>16805,
            "存款理财"=>16809,
            "其他财产"=>16631,
            "债权债务"=>16632,
            "客户诉求"=>16634,
            "被继承人"=>16785,
            "继承人"=>16786,
        ];


        foreach($data['items']['form'] as $k=>$v){

            if(array_key_exists($v['title'],$arr)){
                $answer=DB::table("q_user_answer")->where([
                    ['quid',$quid],
                    ['qpid',$arr[$v['title']]]
                ])->first();
                if($answer){
                    $answerData=DB::table("q_user_answer")->where([
                        ['quid',$quid],
                        ['qpid',$arr[$v['title']]],
                        ['value',$v['content']]
                    ])->first();
                    if(!$answerData){
                        $answer=DB::table("q_user_answer")->where([
                            ['quid',$quid],
                            ['qpid',$arr[$v['title']]]
                        ])->update(['value'=>$v['content']]);
                    }
                }else{
                    DB::table("q_user_answer")->insertGetId([
                        'quid'=>$quid,
                        'qpid'=>$arr[$v['title']],
                        'value'=>$v['content'],
                    ]);
                }
            }
            
        }








        if($data['items']['lawfirmid']){
            if($data['items']['lawfirmid']==3){
                $this->jiben_answer_tongbu_aladin($quid,16624,1);
                $this->jiben_answer_tongbu_aladin($quid,16713,1);
            }else if($data['items']['lawfirmid']==14){
                $this->jiben_answer_tongbu_aladin($quid,16624,2);
                $this->jiben_answer_tongbu_aladin($quid,16713,2);
            }
            else if($data['items']['lawfirmid']==15){
            $this->jiben_answer_tongbu_aladin($quid,16624,3);
            $this->jiben_answer_tongbu_aladin($quid,16713,3);
            }
        }
        if($data['items']['name']){
        $this->jiben_answer_tongbu_aladin($quid,16625,$data['items']['name']);
        $this->jiben_answer_tongbu_aladin($quid,16714,$data['items']['name']);
        }
        
        if($data['items']['sex']){
            if($data['items']['sex']=="女"){
                $this->jiben_answer_tongbu_aladin($quid,16626,2);
                $this->jiben_answer_tongbu_aladin($quid,16715,2);
            }else if($data['items']['sex']=="男"){
                $this->jiben_answer_tongbu_aladin($quid,16626,1);
                $this->jiben_answer_tongbu_aladin($quid,16715,1);
            }
        }
        



    }


    public function jiben_answer_tongbu_aladin($quid,$qpid,$content){
            $answer=DB::table("q_user_answer")->where([
                ['quid',$quid],
                ['qpid',$qpid]
            ])->first();
            if($answer){    
                $answerData=DB::table("q_user_answer")->where([
                    ['quid',$quid],
                    ['qpid',$qpid],
                    ['value',$content]
                ])->first();
                if(!$answerData){
                    $answer=DB::table("q_user_answer")->where([
                        ['quid',$quid],
                        ['qpid',$qpid]
                    ])->update(['value'=>$content]);
                }
            }else{
                DB::table("q_user_answer")->insertGetId([
                    'quid'=>$quid,
                    'qpid'=>$qpid,
                    'value'=>$content,
                ]);
            }
    }



    public function order_flyjs($orderid){
        $token=get_aladin_token();
        $url="https://office365.jialilaw.com/api/order/order_flyjs/orderid/{$orderid}/token/{$token}";
        return json_decode(curl($url),true);
    }


    public function aladin_select_questionnaire(Request $request){
        $data=$request->only('aladin_orderIds');
        $aladin_orderIds=explode(',',$data['aladin_orderIds']);
        $res=DB::table("Q_q_p_users")->whereIn("aladin_orderId",$aladin_orderIds)->select("aladin_orderId","id")->get();
        return [
            'status_code'      => 200,
            'message'     =>  '查询成功',
            'data' => $res
        ];
    }


    /**
     * sop用户新增问卷
     */
    public function sop_add_questionnaire(Request $request,$qid){

        $data = $request->only('qid','projectId','sopTaskId');
        
        $data['createdTime']=date("Y-m-d H:i:s",time());
        $data['updateTime']=date("Y-m-d H:i:s",time());
        $data['userId']=$this->uid;
        $data['agreement_number']=time().rand(1000,9999);

         
        $res = DB::table("Q_q_p_users")->insertGetId($data);

        $this->sop_add_questionnaire_qp_list($qid,$res,$data['projectId']);
        DB::table("Q_q_p_users")->where("id",$res)->update(["history"=>$res]);

        

        if($res){
            DB::table("Q_user_answer")->insertGetId([
                "qpid"=>965,
                "quid"=>$res,
                "value"=>"(2019)京x民初x号",
                'fornum'=>0
            ]);
            
            $questionnaire = DB::table("q_questionnaire")->where("id",$data['qid'])->first();
            // if($questionnaire['Ofqp']){
            //     $Ofqp = json_decode($questionnaire['Ofqp']);

            //     foreach ($Ofqp as $k=>$v){
            //         DB::table("Q_user_answer")->insertGetId(['value'=>1,'qpid'=>$v,'fornum'=>1,'quid'=>$res]);
            //     }

            // }
            $data = $request->only('qid','type','sopTaskId','projectId');
             //判断是否需要导入上次输入 的日志
        
            if($data['type']==1){
                
                $task=DB::connection('mysql2')->table("P_task")->where('id',$data['sopTaskId'])->first();
                // DB::table("Q_user_answer")->insertGetId(['value'=>$task['start_time']?$task['start_time']:date("Y-m-d H:i:s",time()),'qpid'=>DB::table("q_q_p")->where([['questionnaireId',$data['qid']],['problemId',524]])->first()['id'],'fornum'=>0,'quid'=>$res]);
                // DB::table("Q_user_answer")->insertGetId(['value'=>$task['stop_time']?$task['stop_time']:date("Y-m-d H:i:s",time()),'qpid'=>DB::table("q_q_p")->where([['questionnaireId',$data['qid']],['problemId',685]])->first()['id'],'fornum'=>0,'quid'=>$res]);

                $created_time=DB::table("q_q_p")->where([['questionnaireId',$data['qid']],['problemId',524]])->get();
                foreach($created_time as $k=>$v){
                    DB::table("Q_user_answer")->insertGetId(['value'=>date("Y-m-d H:i",time()),'qpid'=>$v['id'],'fornum'=>0,'quid'=>$res]);
                }
                $end_time=DB::table("q_q_p")->where([['questionnaireId',$data['qid']],['problemId',685]])->get();
                foreach($end_time as $k=>$v){
                   DB::table("Q_user_answer")->insertGetId(['value'=>date("Y-m-d H:i",time()),'qpid'=>$v['id'],'fornum'=>0,'quid'=>$res]);
                }

                $sopTaskData = DB::connection('mysql2')->table("sop_blade_task")->where('id',$task['sopTaskId'])->first();
                if($sopTaskData['status_last']==1){
               
                //插入上次输入日志
                $firstQpuid = DB::table('q_q_p_users')->where([['userId',$this->uid],['qid',$data['qid']],['sopTaskId',$data['sopTaskId']],['id',"!=",$res],['projectId',$data['projectId']]])->orderBy('id','desc')->first();
                
                //     var_dump([['userId',$this->uid],['qid',$data['qid']],['sopTaskId',$data['sopTaskId']],['id',"!=",$res],['projectId',$data['projectId']]]);
                //     var_dump($firstQpuid);
                // die;
                if($firstQpuid){
                    
                    $answers = DB::table("Q_user_answer")->where('quid',$firstQpuid)->get()->toArray();
                    
                    
                    foreach($answers as $k=>$v){
                        if($v['qpid']==524) continue;
                        if($v['qpid']==658) continue;
                        $linkValue = $v;
                        unset($linkValue['id']);
                        $linkValue['quid']=$res;
                        DB::table("Q_user_answer")->insertGetId($linkValue);
                    }

                }
                        
             }

            }else{
                
                $created_time=DB::table("q_q_p")->where([['questionnaireId',$data['qid']],['problemId',524]])->get();
                foreach($created_time as $k=>$v){
                    DB::table("Q_user_answer")->insertGetId(['value'=>date("Y-m-d H:i:s",time()),'qpid'=>$v['id'],'fornum'=>0,'quid'=>$res]);
                }
                $end_time=DB::table("q_q_p")->where([['questionnaireId',$data['qid']],['problemId',685]])->get();
                foreach($end_time as $k=>$v){
                   DB::table("Q_user_answer")->insertGetId(['value'=>date("Y-m-d H:i:s",time()),'qpid'=>$v['id'],'fornum'=>0,'quid'=>$res]);
                }
            }

            return [
                'status_code'      => 200,
                'message'     =>  '提交成功',
                'data' => $res
            ];
        }else{
            return [
                'status_code'      => 200,
                'message'     =>  '提交失败 联系后端开发',
                'data' => $res
            ];
        }

    }

    public function sop_add_questionnaire_qp_list($qid,$quid,$projectId){
        $type = DB::table("q_questionnaire")->where("id",$qid)->first()['type'];
        $uid=$this->uid;
        // $type = DB::table("q_questionnaire")->where("id",$qid)->first()['type'];
        // $qpIds = DB::table("q_q_p")->where("questionnaireId",$qid)->get();
        $groups = DB::table("q_type_group")->where("q_type",$type)->get();
        foreach($groups as $k=>$v){
            $group_qpIds=DB::table("q_tg_qp")->where("q_type_group",$v['id'])->get()->toArray();
            $group_qpIds = array_column($group_qpIds,"qpid");
            // die;
            $groupValues = DB::table("q_peoject_qtq")->where("projectId",$projectId)->whereIn("qpid",$group_qpIds)->orderBy("id","desc")->first();
            

            if($groupValues){
                // $newqpIds = DB::table("q_q_p")->where("questionnaireId",$qid)->whereIn("id",$group_qpIds)->get();
                // $insertIds = DB::table("q_user_answer")->where([
                //     ['quid',$quid]
                // ])->whereNotIn("qpid",$newqpIds)->get();
           
                foreach($group_qpIds as $kk=>$vv){
                    DB::table("q_user_answer")->insertGetId([
                        "value"=>$groupValues['value'],
                        "qpid"=>$vv,
                        "fornum"=>0,
                        "quid"=>$quid,
                        "userId"=>$this->uid,
                    ]);
                }
            }
        }
        
    }


    public function user_update_questionnaire(Request $request,$quid){
        $data = $request->only('qid','status','content','title','exemption','complete','signed');
        $data['createdTime']=date("Y-m-d H:i:s",time());
        $data['updateTime']=date("Y-m-d H:i:s",time());

        $res = DB::table("Q_q_p_users")->where("id",$quid)->update($data);


        if($res){
            return [
                'status_code'      => 200,
                'message'     =>  '修改成功',
                'data' => $res
            ];
        }else{
            return [
                'status_code'      => 200,
                'message'     =>  '修改失败 请确认数据是否已修改',
                'data' => $res
            ];
        }
    }

    public function user_update_orderId($quid){
//        $data = DB::table("Q_q_p_users")->where("userId",$this->uid)->orderBy("orderId","desc")->first();
//        $res = DB::table("Q_q_p_users")->where("userId",$this->uid)->decrement("orderId");
        $res = DB::table("Q_q_p_users")->where("id",$quid)->first();
        if($res['orderId']==1){
            $res2 = DB::table("Q_q_p_users")->where("id",$quid)->update(["orderId"=>0]);
        }else{
            $res2 = DB::table("Q_q_p_users")->where("id",$quid)->update(["orderId"=>1]);
        }
        if($res2){
            return [
                'status_code'      => 200,
                'message'     =>  '置顶成功',
                'data' => $res2
            ];
        }else{
            return [
                'status_code'      => 200,
                'message'     =>  '置顶失败 请确认数据是否已修改',
                'data' => $res2
            ];
        }

    }

    public function user_add_answer(Request $request){

        
        $data = $request->only("value","qpid","fornum","quid");
        $data99 = $request->only("value","qpid","projectId","quid");
        if(!array_key_exists("fornum",$data)) $data['fornum']=0;
        $where = [
            [
                'quid',$data['quid']
            ],[
                'qpid',$data['qpid']
            ]
        ];

        if(array_key_exists('fornum',$data)) $where[]=['fornum',$data['fornum']];

        $select_user_answer=DB::table("Q_user_answer")->where($where)->first();

        if($select_user_answer){
            $res = DB::table("Q_user_answer")->where($where)->update($data);
        }else{
            $res = DB::table("Q_user_answer")->insertGetId($data);
        }



        

        if($res){

            $qpDataOnly = DB::table("Q_q_p")->where("id",$data['qpid'])->first();
            $PofP=json_decode($qpDataOnly['PofP']);
            if($qpDataOnly['PofP']&&is_object($PofP)){  //关联与关联的关联 原告性别关联被告性别

                foreach ($PofP as $k=>$v){
                    $where=false;
                    switch($v){
                        case ">":
                            if($data['value']>$v->value)  $where=true;
                        break;
                        case "<":
                            if($data['value']<$v->value)  $where=true;
                        break;
                        case ">=":
                            if($data['value']>=$v->value)  $where=true;
                        break;
                        case "<=":
                            if($data['value']<=$v->value)  $where=true;
                        break;
                        case "==":
                            if($data['value']==$v->value)  $where=true;
                        break;
                    } 

                    if($where){

                        $PofPWhere = DB::table("Q_user_answer")->where([
                            [
                                "quid",$data['quid']
                            ],[
                                "qpid",$v->ofqpid
                            ],[
                                "fornum",$data['fornum']
                            ]
                        ])->first();
                        if($PofPWhere){
                            DB::table("Q_user_answer")->where([
                                [
                                    "quid",$data['quid']
                                ],[
                                    "qpid",$v->ofqpid
                                ],[
                                    "fornum",$data['fornum']
                                ]
                            ])->update([
                                'value'=>$v->ofvalue
                            ]);
                        }else{
                            DB::table("Q_user_answer")->insertGetId([
                                'quid'=>$data['quid'],
                                'uid'=>$this->uid,
                                'qpid'=>$v->ofqpid,
                                'fornum'=>$data['fornum'],
                                'value'=>$v->ofvalue
                            ]);
                        }


                        return [
                            'status_code'      => 202,
                            'message'     =>  '操作成功，请刷新请求',
                            'data' => $res
                        ];
                    }
                }
            }

            if($qpDataOnly['ofCourt']){  //地址关联法院


//                $court = DB::table()
//                 $address = json_encode($data['value']);
                $address=explode(",",str_replace('"',"",str_replace("]","",str_replace("[","",$data['value']))));
                if($address[1]=="市辖区"){
                    $addressAll=$address[0].$address[2];
                }else{
                    $addressAll=$address[1].$address[2];
                }
                $court=DB::table("C_court")->where("name","like","%".$addressAll."%")->first();


                if($court){

                    $courtWhere = DB::table("Q_user_answer")->where([
                        [
                            "quid",$data['quid']
                        ],[
                            "qpid",$qpDataOnly['ofCourt']
                        ],[
                            "fornum",$data['fornum']
                        ]
                    ])->first();

                    if($courtWhere){

                        DB::table("Q_user_answer")->where([
                            [
                                "quid",$data['quid']
                            ],[
                                "qpid",$qpDataOnly['ofCourt']
                            ],[
                                "fornum",$data['fornum']
                            ]
                        ])->update([
                            'value'=>$court['name']
                        ]);
                    }else{
                        DB::table("Q_user_answer")->insertGetId([
                            'quid'=>$data['quid'],
                            'qpid'=>$qpDataOnly['ofCourt'],
                            'fornum'=>$data['fornum'],
                            'value'=>$court['name'],
                            'userId'=>$this->uid
                        ]);
                    }

                    if(array_key_exists('projectId',$data99)) $this->sop_answer($data99);
                    return [
                        'status_code'      => 202,
                        'message'     =>  '操作成功，请刷新请求',
                        'data' => $res
                    ];
                }
            }


            if(array_key_exists('projectId',$data99)) $this->sop_answer($data99);
            return [
                'status_code'      => 200,
                'message'     =>  '操作成功',
                'data' => $res
            ];
        }else{
            return [
                'status_code'      => 200,
                'message'     =>  '修改失败 请确认数据是否已修改',
                'data' => $res
            ];
        }
    }

    public function sop_answer($data){


        $sopData = DB::connection('mysql2')->table('P_project')->where('id',$data['projectId'])->first();
        $sopPid=$sopData['sop_pid'];
        // if()
        if($sopPid){
            $sopProject=DB::connection('mysql2')->table('sop_blade_active')->where('id',$sopPid)->first();

        if($sopData['version']==0){
            DB::connection('mysql2')->table('P_project')->where('id',$data['projectId'])->update(['polymerization'=>$sopProject['polymerization'],'polymerizationQpids'=>$sopProject['polymerizationQpids'],'version'=>$sopProject['version']]);
        }

        $topData=DB::table("q_q_p as qqp")
                ->leftJoin("q_problem as qp","qp.id","=","qqp.problemId")
                ->where('qqp.id',$data['qpid'])->select("qqp.*","qp.type as qpType")->first();

        if(in_array($topData['qpType'],[6,8])){
            $relation=DB::table('q_relation')->where([['qpId',$data['qpid']],['value',"!=",$data['value']]])->get()->toArray();

            foreach($relation as $K=>$v){
                $this->delete_branch($v['relationId'],$data['quid']);
            }
        }else if(in_array($topData['qpType'],[7,9])){
            $relation=DB::table('q_relation')->where('qpId',$data['qpid'])->whereNotIn('value',json_decode($data['value']))->get()->toArray();
            foreach($relation as $K=>$v){
                $this->delete_branch($v['relationId'],$data['quid']);
            }
        }

            $qpids=json_decode($sopProject['noLelvelIds'],true);
            $level=json_decode($sopProject['levelIds'],true);
            $levelId=json_decode($sopProject['levelId'],true);

            // dd($qpids);
            // dd($level);
            // dd($levelId);
            // die;
            if(in_array($data['qpid'],$qpids)||in_array($data['qpid'],$level)){
                $polymerization = $sopData['polymerization'];
                $polymerization=json_decode($polymerization,'true');
                // foreach($polymerization as $k=>$v){
                //     if(!array_key_exists('data',$v)) continue;
                //     foreach($v['data'] as $kk=>$vv){
                //         if($kk==$data['qpid']) $polymerization[$k]['data'][$kk]['answer']=$data['value'];        
                //         // if($kk==$data['qpid']) var_dump($data['value']);
                //     }
                // }

                if(in_array($data['qpid'],$level)){
                
                    if(in_array($data['qpid'],$levelId)){
                        $topLevelId=$data['qpid'];
                    }else{
                        $topIds=DB::table('q_relation')->where('relationId',$data['qpid'])->get()->toArray();
                        $topLevelId=$this->diguiTopLevel($topIds,$levelId);
                    }
    
                    //更新子集文字
                    
                    $text=$this->zijiText($topLevelId,$data['quid']);

                    foreach($polymerization as $k=>$v){
                        if(!array_key_exists('data',$v)) continue;
                        foreach($v['data'] as $kk=>$vv){
                            if($kk==$topLevelId) $polymerization[$k]['data'][$kk]['answer']=$text;
                        }
                    }
                }else{
                    $text=$this->zijiText2($data['qpid'],$data['quid']);
                    foreach($polymerization as $k=>$v){
                        if(!array_key_exists('data',$v)) continue;
                        foreach($v['data'] as $kk=>$vv){
                            if($kk==$data['qpid']) $polymerization[$k]['data'][$kk]['answer']=$text;
                        }
                    }
                }

               

                DB::connection('mysql2')->table('P_project')->where('id',$data['projectId'])->update(['polymerization'=>json_encode($polymerization)]);
                // var_dump($polymerization);
            }
      
            
            
        } 
       
        $data['groupId']=DB::table('q_tg_qp')->where("qpid",$data['qpid'])->first()['q_type_group'];
        $data['uid']=$this->uid;
        if(!array_key_exists("groupId",$data)) return;
        $res = DB::table('q_peoject_qtq')->insertGetId($data);
       
        $qid = DB::table("q_q_p_users")->where("id",$data['quid'])->first()['qid'];
        $this->sop_add_questionnaire_qp_list($qid,$data['quid'],$data['projectId']);

    }

    public function diguiTopLevel($topIds,$levelId){
        $topId=null;
        foreach($topIds as $k=>$v){
            if(in_array($v['qpid'],$levelId)){
                $topId=$v['qpid'];
            }else{
                $topIds=DB::table('q_relation')->where('relationId',$v['qpid'])->get()->toArray();
                $topId=$this->diguiTopLevel($topIds,$levelId);
            }
        }
        return $topId;
    }


    public function zijiText($topLevelId,$quid){
        $text="";
        $topData=DB::table("q_q_p as qqp")
                ->leftJoin("q_problem as qp","qp.id","=","qqp.problemId")
                ->where('qqp.id',$topLevelId)->select("qqp.*","qp.type as qpType")->first();
        $topValue=DB::table("q_user_answer")->where([['qpid',$topLevelId],['quid',$quid]])->orderBy("id","desc")->first();
        $sunData=DB::table('q_relation')->where('qpid',$topLevelId)->get()->toArray();

        $pattern = '/[、）]+/';
        $topData['title']=preg_split($pattern, $topData['title'],2);
        $topData['title']=$topData['title'][count($topData['title'])-1];
        if(!$topValue) return $text;
        if($sunData){
            if(in_array($topData['qpType'],[6,8])){
                $answers=DB::table("q_answer")->where([["problemId",$topData['problemId']],['value',$topValue['value']]])->first();
                $text.=$topData['title']."(".$answers['label'].")";
            }else if(in_array($topData['qpType'],[7,9])){
               
                $answers=DB::table("q_answer")->where([["problemId",$topData['problemId']]])->whereIn('value',json_decode($topValue['value'],true))->get();
                $text.=$topData['title']."(";
                $labelNum=0;
                foreach($answers as $k=>$v){
                    
                    $text.=$labelNum==0?$v['label']:"、".$v['label'];
                    $labelNum++;
                }
                $text.=")/";
            }else{


                $text.=$topData['title']."(".$topValue['value'].")";
            }
            $newFirst = DB::table("q_user_answer")->where('quid',$quid)->whereIn('qpid',array_column($sunData,'relationId'))->orderBy("id",'desc')->first();
            $text.=$this->zijiText($newFirst['qpid'],$quid);
        }else{
            $text.=$topData['title']."(".$topValue['value'].")";
        }
       
        return $text;
    }

    public function delete_branch($qpid,$quid){
        $sunData=DB::table('q_relation')->where('qpid',$qpid)->get()->toArray();
        DB::table('q_user_answer')->where([['quid',$quid],['qpid',$qpid]])->delete();
        
        foreach($sunData as $k=>$v){
            DB::table('q_user_answer')->where([['quid',$quid],['qpid',$v['relationId']]])->delete();
            $this->delete_branch($v['relationId'],$quid);
        }
    }


    public function zijiText2($topLevelId,$quid){

        
        $text="";
        $topData=DB::table("q_q_p as qqp")
                ->leftJoin("q_problem as qp","qp.id","=","qqp.problemId")
                ->where('qqp.id',$topLevelId)->select("qqp.*","qp.type as qpType")->first();
        $topValue=DB::table("q_user_answer")->where([['qpid',$topLevelId],['quid',$quid]])->orderBy("id","desc")->first();

        if(in_array($topData['qpType'],[6,8])){
            $answers=DB::table("q_answer")->where([["problemId",$topData['problemId']],['value',$topValue['value']]])->first();
            $text=$answers['label'];
        }else if(in_array($topData['qpType'],[7,9])){
           
            $answers=DB::table("q_answer")->where([["problemId",$topData['problemId']]])->whereIn('value',json_decode($topValue['value'],true))->get();
            $labelNum=0;
            foreach($answers as $k=>$v){
                
                $text.=$labelNum==0?$v['label']:"、".$v['label'];
                $labelNum++;
            }
        }else{


            $text=$topValue['value'];
        }
       
        return $text;
    }


//
    public function sop_answer_list($projectId){
        $qu=DB::table("q_q_p_users")->where("sop_project",$projectId)->get();
        $quIds=array_column("id",$qu);
    }


    public function user_add_select_answer(Request $request){
        $data = $request->only('quid','qpid');
        if(!array_key_exists('qpid',$data)||!preg_match("/^\d*$/",$data['qpid'])) return ThrowApiExc('32024');
        $res = DB::table("Q_q_p as qqp")  //获取值等信息
            ->select("qua.value","qua.id","qqp.id as qpid","qp.id as problemId")
            ->leftJoin("Q_user_answer as qua","qua.qpid","=","qqp.id")
            ->leftJoin("Q_problem as qp","qp.id","=","qqp.problemId")
            ->where([
                [
                    'qqp.id',$data['qpid']
                ],[
                    'qua.quid',$data['quid']
                ]
            ])->first();
        if($res){  //有值 +1


            $select_answer_label = DB::table("Q_answer")->where([
                [
                    "problemId",$res['problemId']
                ],[
                    "label",">",$res['value']
                ]
            ])->first();
            if($select_answer_label){  //是否有更多选项
                $value = $res['value']+1;  //有加1
                $data['value']=$value;
                
                $res2=DB::table("Q_user_answer")->where("id",$res['id'])->update(['value'=>$value]);
            }else{
                return ThrowApiExc('42335');//无 报错
            }



        }else{ //无值 新增记录
            $res2=DB::table("Q_user_answer")->insertGetId([
                "value"=>1,
                "qpid"=>$data['qpid'],
                "quid"=>$data['quid'],
                'userId'=>$this->uid
            ]);
            $data['value']=1;
        }

        $qpDataOnly = DB::table("Q_q_p")->where("id",$data['qpid'])->first();


            if($qpDataOnly['PofP']){  //关联与关联的关联 原告性别关联被告性别
                $PofP=json_decode($qpDataOnly['PofP']);

                foreach ($PofP as $k=>$v){


                    $where=false;
                    switch($v->type){
                        case "3":
                            if($data['value']>$v->value)  $where=true;
                        break;
                        case "2":
                            if($data['value']<$v->value)  $where=true;
                        break;
                        case "4":
                            if($data['value']>=$v->value)  $where=true;
                        break;
                        case "5":
                            if($data['value']<=$v->value)  $where=true;
                        break;
                        case "1":
                            if($data['value']==$v->value)  $where=true;
                        break;
                    } 

                    if($where){

                        $PofPWhere = DB::table("Q_user_answer")->where([
                            [
                                "quid",$data['quid']
                            ],[
                                "qpid",$v->ofqpid
                            ]
                        ])->first();
                        if($PofPWhere){
                            DB::table("Q_user_answer")->where([
                                [
                                    "quid",$data['quid']
                                ],[
                                    "qpid",$v->ofqpid
                                ]
                            ])->update([
                                'value'=>$v->ofvalue
                            ]);
                        }else{
                            DB::table("Q_user_answer")->insertGetId([
                                'quid'=>$data['quid'],
                                'qpid'=>$v->ofqpid,
                                'value'=>$v->ofvalue,
                                'userId'=>$this->uid
                            ]);
                        }

                    }
                }
            }

        return [
            'status_code'      => 200,
            'message'     =>  '操作成功',
            'data' => $res2
        ];

    }



    public function user_delete_select_answer(Request $request){

        $data = $request->only('quid','qpid','fornum');
        if(!array_key_exists('qpid',$data)||!preg_match("/^\d*$/",$data['qpid'])) return ThrowApiExc('32024');
        if(!array_key_exists('fornum',$data)||!preg_match("/^\d*$/",$data['fornum'])) return ThrowApiExc('32026');

        $res = DB::table("Q_q_p as qqp")  //获取值等信息
        ->select("qua.value","qua.id","qqp.id as qpid","qp.id as problemId")
            ->leftJoin("Q_user_answer as qua","qua.qpid","=","qqp.id")
            ->leftJoin("Q_problem as qp","qp.id","=","qqp.problemId")
            ->where([
                [
                    'qqp.id',$data['qpid']
                ],[
                    'qua.quid',$data['quid']
                ]
            ])->first();

        if($res){

            $qpData = DB::table("Q_q_p")->get()->toArray();
            $qpArr=$this->select_qpid($qpData,$data['qpid']);

            if($res['value']==1){
                $res2=DB::table("Q_user_answer")->where("id",$res['id'])->delete();

                DB::table("Q_user_answer")->where('fornum',$data['fornum'])->whereIn("qpid",$qpArr)->delete();

            }else{
                $value = $res['value']-1;
                $res2=DB::table("Q_user_answer")->where("id",$res['id'])->update(['value'=>$value]);
                DB::table("Q_user_answer")->where('fornum',$data['fornum'])->whereIn("qpid",$qpArr)->delete();
                DB::table("Q_user_answer")->where('fornum',">",$data['fornum'])->whereIn("qpid",$qpArr)->decrement('fornum');
            }


            return [
                'status_code'      => 200,
                'message'     =>  '操作成功',
                'data' => $res2
            ];

        }else{
            return [
                'status_code'      => 200,
                'message'     =>  '啥都没有你请求啥',
                'data' => ""
            ];
        }

    }

    public function select_qpid($data,$qpid){
        $qpidArr=[];
        foreach ($data as $k=>$v){
            if($v['fqaspId']==$qpid){
                $qpidArr[]=$v['id'];
                $newqpidArr = $this->select_qpid($data,$v['id']);
                if($newqpidArr) $qpidArr=array_merge($qpidArr,$newqpidArr);
            }
        }
        return $qpidArr;
    }

    public function get_only_value(Request $request){
        $quid=$request->quid;
        $qpid=$request->qpid;
        $res = DB::table("Q_user_answer")->where([
            [
                "quid",$quid
            ],
            [
                "qpid",$qpid
            ]
        ])->first();

        if($res){
            return [
                'status_code'      => 200,
                'message'     =>  '请求成功',
                'data' => $res['value']
            ];
        }else{
            return [
                'status_code'      => 200,
                'message'     =>  '未填写',
                'data' => ""
            ];
        }
    }

    public function returnQuestionnaireJson(Request $request,$qpid){

        

        $quid = $request->quid;
        $qid = $request->qid;
        $master = DB::table("Q_q_p")->select("id","title","type","problemId","fqaspId")->where("id",$qpid)->first();

        $data = DB::table("Q_q_p as qqp")
            ->select("qqp.id","qqp.title","qqp.type","qp.imgs","qqp.description","qqp.problemId","qqp.fqaspId",'qqp.important',"qp.title as problemTitle",'qp.type as problemType','qp.re','qp.id as problemId',"qp.placeholder as placeholder")
            ->leftJoin("Q_problem as qp","qp.id","=","qqp.problemId")
            ->where("qqp.questionnaireId",$qid)
            ->orderBy("qqp.orderId")
            ->get()->toArray();
        
        

        foreach ($data as $k=>$v){
            $selArr = [6,7,8,9];
            if(in_array($v['problemType'],$selArr)){
                $data[$k]['listData']=DB::table("Q_answer")->select("value","label")->where("problemId",$v['problemId'])->orderBy("value")->get();
            }
            if($v['problemType']==1){
                $data[$k]['input_type']="text";
            }

            if($v['re']==2){
                $data[$k]['input_type']="number";
            }else if($v['re']==3){
                $data[$k]['input_type']="IdCard";
            }else if($v['re']==4){
                $data[$k]['input_type']="BankCard";
            }else if($v['re']==5){
                $data[$k]['input_type']="PhoneNum";
            }else if($v['re']==6){
                $data[$k]['input_type']="textarea";
            }
        }
        

        if($master['type']==3){


            $value=DB::table("Q_user_answer as qua")->where([
                    [
                        "qua.qpid",$qpid
                    ]
                    ,
                    [
                        "qua.quid",$quid
                    ]
            ])->first();


            if($value){



                for($i=1;$i<=$value['value'];$i++){

                    $data1 = $this->return_json($data,$master['id'],$quid,$i);

                    $master['data'][]=$data1;

                }

            }


        }else{
            $data1 = $this->return_json($data,$master['id'],$quid);

            $master['data'][]=$data1;
        }



        return $master;

    }

   
    public function return_json($data,$qpid,$quid,$fornum=false){


        $newData = [];
        foreach ($data as $k=>$v){
            if($v['fqaspId']==$qpid){
                $newData[] = [
                    'title'=>$v['title'],
                    'questions'=>$fornum==false?$this->return_questions_json($data,$v['id'],$quid):$this->return_questions_json($data,$v['id'],$quid,$fornum)
                ];
            }
        }
        return $newData;
    }
//
    public function return_questions_json($data,$qpid,$quid,$fornum=false){




        $newData = [];

        foreach ($data as $k=>$v){
            if($v['fqaspId']==$qpid){
//                $newData[] = $v;
                switch ($v['type']){
                    case 1:
                        $linshi = [
                            'title'=>$v['title'],
                        ];

                    $newData[]=$linshi;
                        break;
                    case 2:
                        $requireQidAndAnswer=false;
                        $linshi = [
                            'title'=>$v['title']??$v['problemTitle'],
                            'id'=>$v['id'],
                            'isRequired'=>$v['important']==1?true:false,
                            'type'=>$v['problemType']==11?"question":create(['problemType2'])['problemType2'][$v['problemType']],
                            'problemId'=>$v['problemId'],
                            "answer"=>$fornum==false?$this->get_answer_value($quid,$v['problemType'],$v['id']):$this->get_answer_value($quid,$v['problemType'],$v['id'],$fornum)
                        ];
                        $selArr = [6,7,8,9];
                        if(in_array($v['problemType'],$selArr)){
                            $linshi['listData']=$v['listData'];

                        }

                        if($v['description']){
                            $linshi['description']=$v['description'];
                        }

                        if($v['imgs']){
                            $linshi['imgDate']=json_decode($v['imgs']);
                        }

                        $selArr2=[1];
                        if(in_array($v['problemType'],$selArr2)){
                            $linshi['input_type']=$v['input_type'];
                        }
                        if($v['placeholder']){
                            $linshi['placeholder']=$v['placeholder'];
                        }

                        if($fornum)  $linshi['fornum']=$fornum;
                        $baohan=$this->baohan($v['id']);

                        $requireQidAndAnswer = $this->requireQidAndAnswer($v['id'],$v['fqaspId']);



                        $childQuestion = $this->return_childQuestion_json($data,$v['id'],$quid,$fornum);



                        if($childQuestion){
                            if($linshi['type']=="question"){
                                $linshi['childQuestion']=$childQuestion;
                            }else{

                                $newChildQuestion = [];
                                $list = DB::table("Q_relation")->where("qpid",$v['id'])->get();
                                if($list){
                                        foreach ($childQuestion as $kkk=>$vvv){
                                            foreach ($list as $kk=>$vv){
                                                if($vv['relationId']==$vvv['id']){

                                                    if($baohan&&$v['problemType']==6){
                                                        foreach($baohan['data'] as $k=>$v){
                                                            $newChildQuestion[$v][]=$vvv;
                                                        }  

                                                    }else{
                                                        $newChildQuestion[$vv['value']][]=$vvv;
                                                    }


                                                }
                                        }
                                    }
                                    $linshi['childQuestion']=$newChildQuestion;
                                }else{
                                    $linshi['childQuestion']=$childQuestion;
                                }


                            }

                        }

                        if($requireQidAndAnswer==true){

                        foreach($requireQidAndAnswer as $ke=>$va){
                            $linshi['requireQidAndAnswer']=['id'=>$va['qpid'],'answer'=>$va['value']];
                            $newData[]=$linshi;
                        }

                        }else{
                            $newData[]=$linshi;
                        }


                        break;
                }

            }
        }
        return $newData;
    }


    public function return_childQuestion_json($data,$qpid,$quid,$fornum=0){
        $newData = [];

        foreach ($data as $k=>$v){

            if($v['fqaspId']==$qpid){
                switch ($v['type']){
                    case 1:
                        $linshi = [
                            'title'=>$v['title'],
                            'id'=>$v['id']
                        ];
                        $childQuestion = $this->return_grandson_json($data,$v['id'],$quid,$fornum);



                        $linshi['questions']=$childQuestion;

                        $newData[]=$linshi;
                        break;
                    case 2:
                        $requireQidAndAnswer=false;
                        $linshi = [
                            'title'=>$v['title']??$v['problemTitle'],
                            'id'=>$v['id'],
                            'isRequired'=>$v['important']==1?true:false,
                            'type'=>$v['problemType']==11?"question":create(['problemType2'])['problemType2'][$v['problemType']],
                            'problemId'=>$v['problemId'],
                            "answer"=>$fornum==false?$this->get_answer_value($quid,$v['problemType'],$v['id']):$this->get_answer_value($quid,$v['problemType'],$v['id'],$fornum)
                        ];
                        $selArr = [6,7,8,9];
                        if(in_array($v['problemType'],$selArr)){
                            $linshi['listData']=$v['listData'];
                        }
                        if($v['imgs']){
                            $linshi['imgDate']=json_decode($v['imgs']);
                        }
                        $selArr2=[1];
                        if(in_array($v['problemType'],$selArr2)){
                            $linshi['input_type']=$v['input_type'];
                        }
                        if($v['description']){
                            $linshi['description']=$v['description'];
                        }
                        if($v['placeholder']){
                            $linshi['placeholder']=$v['placeholder'];
                        }

                        if($fornum)  $linshi['fornum']=$fornum;
                        $baohan=$this->baohan($v['id']);
                        $requireQidAndAnswer = $this->requireQidAndAnswer($v['id'],$v['fqaspId']);

                        $childQuestion = $this->return_grandson_json($data,$v['id'],$quid,$fornum);

                        if($childQuestion){
                            if($linshi['type']=="question"){
                                $linshi['grandson']=$childQuestion;
                            }else{
                                $newChildQuestion = [];
                                $list = DB::table("Q_relation")->where("qpid",$v['id'])->get();
                                if($list){
                                        foreach ($childQuestion as $kkk=>$vvv){
                                            foreach ($list as $kk=>$vv){
                                                if($vv['relationId']==$vvv['id']){
                                                        $newChildQuestion[$vv['value']][]=$vvv;
                                            }
                                        }
                                    }
                                    $linshi['grandson']=$newChildQuestion;
                                }else{
                                    $linshi['grandson']=$childQuestion;
                                }


                            }

                        }

                        if($requireQidAndAnswer==true){

                            foreach($requireQidAndAnswer as $ke=>$va){
                                $linshi['requireQidAndAnswer']=['id'=>$va['qpid'],'answer'=>$va['value']];
                                $newData[]=$linshi;
                            }

                            } else{
                                $newData[]=$linshi;
                            }
                        break;
                }
            }

        }
        return $newData;
    }

    public function return_grandson_json($data,$qpid,$quid,$fornum=false){

        $newData = [];
        foreach ($data as $k=>$v){
            if($v['fqaspId']==$qpid){
                $linshi=[];
                $linshi['id']=$v['id'];
                $linshi['title']=$v['title'];
                if(array_key_exists('input_type',$v)) $linshi['input_type']=$v['input_type'];
                $linshi['isRequired']=$v['important']==1?true:false;
                $linshi['type']=$v['problemType']==11?"question":create(['problemType2'])['problemType2'][$v['problemType']];

                $linshi["answer"]=$fornum==false?$this->get_answer_value($quid,$v['problemType'],$v['id']):$this->get_answer_value($quid,$v['problemType'],$v['id'],$fornum);
                if($fornum)  $linshi['fornum']=$fornum;
                if($v['placeholder']){
                    $linshi['placeholder']=$v['placeholder'];
                }
                if($v['description']){
                    $linshi['description']=$v['description'];
                }
                $selArr = [6,7,8,9];
                if(in_array($v['problemType'],$selArr)){
                    $linshi['listData']=$v['listData'];
                }
                if($v['imgs']){
                    $linshi['imgDate']=json_decode($v['imgs']);
                }

                $requireQidAndAnswer = $this->requireQidAndAnswer($v['id'],$v['fqaspId']);

                if($requireQidAndAnswer==true){

                    foreach($requireQidAndAnswer as $ke=>$va){
                        $linshi['requireQidAndAnswer']=['id'=>$va['qpid'],'answer'=>$va['value']];
                        $newData[]=$linshi;
                    }
                    unset($requireQidAndAnswer);
                }else{
                    $newData[]=$linshi;
                }

            }
        }
        return $newData;
    }

    public function demodemodemo(){
        $res = DB::table("Q_q_p")
            ->where([
                [
                    'type',2
                ],[
                    'problemId',null
                ]
            ])
            ->get();

        foreach ($res as $k=>&$v){
            $problemId = DB::table("Q_problem")->where("title",$v['title'])->first()['id'];
            DB::table("Q_q_p")->where("id",$v['id'])->update([
                'problemId'=>$problemId
            ]);
        }
        return $res;
    }
    //获取用户的问卷的答案
    public function get_answer_value($quid,$problemType,$qpid,$fornum=false){

        $where=[
            [
                "quid",$quid
            ],[
                "qpid",$qpid
            ]
        ];

        if($fornum) $where[]=["fornum",$fornum];

        $res = DB::table("Q_user_answer")->where($where)->first();

        $arr=[6,8,8,9];


        if($res){
            if(preg_match("/^[\d]{1,20}$/u",$res['value'])&&$problemType!=1){
                return intval($res['value']);

            }else{
                return $res['value'];
            }

        }else{
            return "";
        }
    }

    public function requireQidAndAnswer($qpid,$fqaspId){

        $res = DB::table("Q_relation as qr")
            ->leftJoin("Q_q_p as qqp","qr.qpid","=","qqp.id")
            ->select("qr.qpid as qpid","qr.value as value","qr.type","qqp.problemId")
            ->where([
            [
                'qr.relationId',$qpid
            ],[
                'qqp.fqaspid',$fqaspId
            ]
        ])->get();

        $newres = [];
        foreach($res as $k=>$v){
            switch($v['type']){
                case 1:
                    $newres[]=$v;
                break;
                case 6:

                    $answers = DB::table("Q_answer")->where("problemId",$v['problemId'])->get();

                    foreach($answers as $kk=>$vv){
                        if($v['value']!=$vv['value']){
                            $newres[]=['qpid'=>$v['qpid'],"value"=>$vv['value']];
                        }
                    }

                break;
            }
        }

        if($newres!=[]){
            return $newres;
        }else{
            return false;
        }

    }

    public function baohan($qpid){

        $res =DB::table("Q_relation as qr")
        ->select("qr.type as qrType","qr.relationId","qqp.title","qqp.id","qr.value","qp.id as problemId")
        ->leftJoin("Q_q_p as qqp","qr.qpid","=","qqp.id")
        ->leftJoin("Q_problem as qp","qp.id","=","qqp.problemId")
        ->where([
            ["qpid",$qpid]
        ])
        ->first();

        $valueData = DB::table("q_answer")->select("value")->where("problemId",$res['problemId'])->get();

        $value=["id"=>$res['relationId'],"data"=>[]];


        foreach ($valueData as $k=>$v) {
            switch ($res['qrType']) {

                case 1://等于
                    if($res['value']==$v['value']){
                        $value['data'][]=$v['value'];
                    }
                break;
                case 2://小于
                    # code...
                    if($res['value']>$v['value']){
                        $value['data'][]=$v['value'];
                    }
                    break;
                case 3://大于
                    if($res['value']<$v['value']){
                        $value['data'][]=$v['value'];
                    }
                    # code...
                    break;
                case 4://大于等于
                    if($res['value']<=$v['value']){
                        $value['data'][]=$v['value'];
                    }
                    # code...
                    break;
                case 5://小于等于
                    if($res['value']>=$v['value']){
                        $value['data'][]=$v['value'];
                    }
                    # code...
                    break;
                case 6://不等于
                    if($res['value']!=$v['value']){
                        $value['data'][]=$v['value'];
                    }
                    # code...
                break;
                // case 7://包含
                //     if($res['value']=$v['value']){
                //         $value[]=$v['value'];
                //     }
                //     # code...
                // break;
            }
        }



        return $value;

    }


    public function get_word($quid){

        $word_text=DB::table("Q_q_p_users")->where("id",$quid)->first()['content'];
        $phpWord=new PhpWord();



        $phpWord->setDefaultFontName('仿宋');//字体
        $phpWord->setDefaultFontSize(12);//字号
        //添加页面
        $section = $phpWord->createSection();

        $wordData=DB::table("Q_q_p_users as qqpu")
            ->leftJoin("Q_questionnaire as qq","qq.id","=","qqpu.qid")
            ->select("qq.title","qqpu.qid","qq.type")
            ->where("qqpu.id",$quid)
            ->first();

        $wordName=$wordData['title'];

        $phpWord->addTitleStyle(1, ['bold' => true, 'color' => '000', 'size' => 13.5, 'name' => 'Verdana'],['align'=>"center"]);
        $section->addTitle($wordName, 1);


//        $word = new WordController($request);
//        $word_text = $word->outputWord($quid);

        $textrun = $section->createTextRun();

        $textrun->addTextBreak();//换行符


        $newData = explode("\n",$word_text);


        foreach ($newData as $k=>$v){

            if(strpos($v,"<b>")!== false){
//                preg_match_all("/>(.*?)</is",$v,$arr);


                $asr=explode("</b>",$v);

                $textrun->addText(htmlspecialchars(preg_replace("/[|]+/","",str_replace("<b>","",$asr[0]))),['bold' => true]);
                $textrun->addText(htmlspecialchars(preg_replace("/[|]+/","",$asr[1])));
            }else{
                $textrun->addText(htmlspecialchars(preg_replace("/[|]+/","",str_replace("      ","  ",$v))));
            }
            $textrun->addTextBreak();//换行符
        }
        switch ($wordData['type']){
            case 1:
                $textrun->addText("     男方（签字）：                      女方（签字）：");
                $textrun->addTextBreak();//换行符
                $textrun->addTextBreak();//换行符
                $textrun->addText("                      年   月   日                       年   月   日");
                $textrun->addTextBreak();//换行符
                $textrun->addTextBreak();//换行符
                break;
            case 2:
                $textrun->addText("                                               具状人：");
                $textrun->addTextBreak();//换行符
                $textrun->addTextBreak();//换行符
                $textrun->addText("                                                         年   月   日");
                $textrun->addTextBreak();//换行符
                $textrun->addTextBreak();//换行符
                break;
            case 3:
                $textrun->addText("                                               申请人：");
                $textrun->addTextBreak();//换行符
                $textrun->addTextBreak();//换行符
                $textrun->addText("                                                         年   月   日");
                $textrun->addTextBreak();//换行符
                $textrun->addTextBreak();//换行符
                break;
        }


        $writer = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
        $timename=date("YmdHis",time());


        $filename='./word/'.$timename.$wordName.'.docx';
        $writer->save($filename);
        $name = basename($filename);
//        return response()->download($filename, $name ,$headers = ['Content-Type'=>'application/zip;charset=utf-8']);
        DB::table("Q_q_p_users")->where("id",$quid)->update([
            "wordFilePath"=>$timename.$wordName.'.docx'
        ]);
        return [
            'status_code'      => 200,
            'message'     =>  '查询成功',
            'data' => $timename.$wordName.'.docx'
            ];


    }

    public function get_wordTS($quid){

        $word_text=DB::table("Q_q_p_users")->where("id",$quid)->first()['content'];
        $phpWord=new PhpWord();

        $phpWord->setDefaultParagraphStyle(
            array(
                'spacing'    => 120
                )
            );

        $law=DB::table("q_user_answer as qua")
        ->where([
            ['quid',$quid]
        ])->whereIn('qpid',[16624,16713])->first();


        $phpWord->setDefaultFontName('微软雅黑');//字体
        $phpWord->setDefaultFontSize(14);//字号
        //添加页面
        $section2 = $phpWord->createSection();
        $section = $phpWord->createSection();
        
        $phpWord->addFontStyle("myOwnStyle",[
            "spaceBefore"=>26,
            "spacing"=>43,
        ]);


        $sectionStyle = $section2->getSettings();
        $sectionStyle->setMarginTop(0);
        $sectionStyle->setMarginLeft(0);
        $sectionStyle->setMarginRight(0);
        $sectionStyle->setMarginBottom(0);
    

        switch($law['value']){
            case 1:
                $section2->addImage("./ft/bjHeader.jpg",[
                    'width'=>600,
                    'align'=>'center'
                ]);
            break;
            case 2:
                $section2->addImage("./ft/szHeader.jpg",[
                    'width'=>600,
                    'align'=>'center'
                ]);
            break;
            case 3:
                $section2->addImage("./ft/shHeader.jpg",[
                    'width'=>600,
                    'align'=>'center'
                ]);
            break;
        }

        $wordData=DB::table("Q_q_p_users as qqpu")
            ->leftJoin("Q_questionnaire as qq","qq.id","=","qqpu.qid")
            ->select("qq.title","qqpu.qid","qq.type")
            ->where("qqpu.id",$quid)
            ->first();

        $wordName=$wordData['title'];

        $phpWord->addTitleStyle(1, ['bold' => true, 'color' => '000', 'size' => 13.5, 'name' => 'Verdana'],['align'=>"center"]);
        // $section->addTitle($wordName, 1);
        
        $footer = $section->createFooter();
        $footer -> addText("家理，专注婚姻家事法律服务",[
            'color'=>"cc0e4b",
            'size' => 11            
        ],[
            'align'=>'center'

        ]);

        $header = $section->createHeader();
        $header -> addImage("./header.png",[
            'width'=>200
        ]);

//        $word = new WordController($request);
//        $word_text = $word->outputWord($quid);

        $textrun = $section->createTextRun("myOwnStyle");



        $newData = explode("\n",$word_text);

        $num=0;
        foreach ($newData as $k=>$v){
            // $num++;
            if(strpos($v,"<b>")!== false){
//                preg_match_all("/>(.*?)</is",$v,$arr);


                $asr=explode("</b>",$v);

                $textrun->addText(htmlspecialchars(preg_replace("/[|]+/","",str_replace("<b>","",$asr[0]))));
                $textrun->addText(htmlspecialchars(preg_replace("/[|]+/","",$asr[1])));
            }else{

                

                if(strstr($v,"{{7}}")){
                    $num=16;
                    $tuandui=DB::table("q_tb")->where(['quid'=>$quid,'type'=>1])->first();
                    $zhuban=DB::table("q_tb")->where(['quid'=>$quid,'type'=>2])->first();

                    if($tuandui){
                        $textrun->addText("■ 团队负责人介绍");
                        $textrun->addTextBreak();//换行符
            
                        $textrun->addImage($tuandui['img'],[
                            'width'=>450,
                            'align'=>'center'
                        ]);
                   }

                    if($zhuban){
                        $textrun->addTextBreak();//换行符

                        $textrun->addText("■ 主办律师介绍");
            
                        $textrun->addImage($zhuban['img'],[
                            'width'=>450,
                            'align'=>'center'
                        ]);

                    }

               
                    continue;

                }else if(strstr($v,"{{8")){
                    preg_match("~\{\{\s*(.*?)\s*\}\}~", $v, $arr);
                    $num++;
                    $textrun->addText(explode(",",$arr[1])[1],[
                        'color'=>"cc0e4b",
                        'size'=>14,
                        'bold'=>true
                    ]);
                }else if(strstr($v,"{{16")){
                    preg_match("~\{\{\s*(.*?)\s*\}\}~", $v, $arr);
                    $num=$num+ceil(strlen(explode(",",$arr[1])[1])/99);
                    $textrun->addText(explode(",",$arr[1])[1]);
                }else if(strstr($v,"{{9}}")){
                        if($num<=1){
                            $num=0;
                        }else if($num<=17){
                            $num=0;
                            $section->addPageBreak();

                            $textrun= $section->createTextRun();
                        }else if($num==18){
                            $num=0;
                            $textrun= $section->createTextRun();
                        }else{
                            $num=0;
                        }

                        

                        continue; 
                        //
                }else if(strstr($v,"{{14}}")){
                    $num=16;
                    $textrun->addImage("./ft/5.png",[
                        "width"=>450
                    ]);
                      

                }else if(strstr($v,"<noBold>")){
                    preg_match("~\<noBold\>\s*(.*?)\s*\</noBold\>~", $v, $arr);
                    $textrun->addText($arr[1]);
                    $num=$num+ceil(strlen($arr[1])/99);
                    // dd(ceil(strlen($arr[1])/99));

                }else if(strstr($v,"<jiacu>")){
                    preg_match("~\<jiacu\>\s*(.*?)\s*\</jiacu\>~", $v, $arr);
                    $textrun->addText("       ".$arr[1],['bold'=>true]);
                    $str=$arr[1];
                    $arr=explode("</jiacu>",$v);
                    $textrun->addText($arr[1]);
                    $str.=$arr[1];
                    $num=$num+ceil(strlen($str)/97);

//
                }
                else{
                    $num=$num+ceil(strlen("       ".$v)/98);
                    $textrun->addText("       ".htmlspecialchars(preg_replace("/[|]+/","",str_replace("      ","  ",$v))));
                   
                }

                // var_dump(count($newData),$k,$v,$num,strlen("       ".$v));
                if($num==19){
                    $num=0;
                    if($k+1==count($newData)){
                        continue;
                    }
                    $textrun= $section->createTextRun("myOwnStyle");
                    continue;
                }else if($num>19){
                    $num=$num-19;
                }

            }
            $textrun->addTextBreak();//换行符
        }



        $section3 = $phpWord->createSection();

        // $sectionStyle = $section3->getSettings();
        // $sectionStyle->setMarginTop(0);
        // $sectionStyle->setMarginLeft(0);
        // $sectionStyle->setMarginRight(0);
        // $sectionStyle->setMarginBottom(0);


        // switch($law['value']){
        //     case 1:
                $section3->addImage("./ft/footer.jpg",[
                    'width'=>460,
                    'align'=>'center'
                ]);
        //     break;
        //     case 2:
        //         $section3->addImage("./ft/szFooter.jpg",[
        //             'width'=>600,
        //             'align'=>'center'
        //         ]);
        //     break;
        //     case 3:
        //         $section3->addImage("./ft/shFooter.jpg",[
        //             'width'=>600,
        //             'align'=>'center'
        //         ]);
        //     break;
        // }
        $timename=date("YmdHis",time());


        $filename='./word/'.$timename.$wordName.'.docx';
        // $filenamePDF='./word/'.$timename.$wordName.'.pdf';



        $writer = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');

   
        $writer->save($filename);
        $name = basename($filename);
//        return response()->download($filename, $name ,$headers = ['Content-Type'=>'application/zip;charset=utf-8']);
        DB::table("Q_q_p_users")->where("id",$quid)->update([
            "wordFilePath"=>$timename.$wordName.'.docx'
        ]);

        // $phpWord = \PhpOffice\PhpWord\IOFactory::load($timename.$wordName.'.docx');
        // $pdf = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'PDF');
        // $pdf->save($filenamePDF);

        // $namePDF = basename($filenamePDF);

        // $this->word_pdfTS('./word/'.$timename.$wordName.'.docx','./word/'.$timename.$wordName,$phpWord);

        return [
            'status_code'      => 200,
            'message'     =>  '查询成功',
            'data' => $timename.$wordName.'.docx',
            // 'data2'=>$filenamePDF.$wordName.'.pdf',
            ];
    }

    public function word_pdfTS($quid){

        $word_text=DB::table("Q_q_p_users")->where("id",$quid)->first()['content'];
        $phpWord=new PhpWord();

        $phpWord->setDefaultParagraphStyle(
            array(
                'spacing'    => 120
                )
            );

        $law=DB::table("q_user_answer")->where([
            ['quid',$quid],
            ['qpid',16624]
        ])->first();


        $phpWord->setDefaultFontName('MicrosoftYaHei-01');//字体
        $phpWord->setDefaultFontSize(14);//字号
        // $phpWord->setpageSizeW('600');
        //添加页面
        $section2 = $phpWord->createSection();
        $section = $phpWord->createSection();
        
        $phpWord->addFontStyle("myOwnStyle",[
            "spaceBefore"=>26,
            "spacing"=>43,
        ]);


        $sectionStyle = $section2->getSettings();
        $sectionStyle->setMarginTop(0);
        $sectionStyle->setMarginLeft(0);
        $sectionStyle->setMarginRight(0);
        $sectionStyle->setMarginBottom(0);
    

        switch($law['value']){
            case 1:
                $section2->addImage("./ft/bjHeader.jpg",[
                    'width'=>600,
                    'align'=>'center'
                ]);
            break;
            case 2:
                $section2->addImage("./ft/szHeader.jpg",[
                    'width'=>600,
                    'align'=>'center'
                ]);
            break;
            case 3:
                $section2->addImage("./ft/shHeader.jpg",[
                    'width'=>600,
                    'align'=>'center'
                ]);
            break;
        }

        $wordData=DB::table("Q_q_p_users as qqpu")
            ->leftJoin("Q_questionnaire as qq","qq.id","=","qqpu.qid")
            ->select("qq.title","qqpu.qid","qq.type")
            ->where("qqpu.id",$quid)
            ->first();

        $wordName=$wordData['title'];

        $phpWord->addTitleStyle(1, ['bold' => true, 'color' => '000', 'size' => 13.5, 'name' => 'Verdana'],['align'=>"center"]);
        // $section->addTitle($wordName, 1);
        
        $footer = $section->createFooter();
        $footer -> addText("家理，专注婚姻家事法律服务",[
            'color'=>"cc0e4b",
            'size' => 11            
        ],[
            'align'=>'center'

        ]);

        $header = $section->createHeader();
        $header -> addImage("./header.png",[
            'width'=>200
        ]);

//        $word = new WordController($request);
//        $word_text = $word->outputWord($quid);

        $textrun = $section->createTextRun("myOwnStyle");



        $newData = explode("\n",$word_text);

        $num=0;
        foreach ($newData as $k=>$v){
            // $num++;
            if(strpos($v,"<b>")!== false){
//                preg_match_all("/>(.*?)</is",$v,$arr);


                $asr=explode("</b>",$v);

                $textrun->addText(htmlspecialchars(preg_replace("/[|]+/","",str_replace("<b>","",$asr[0]))));
                $textrun->addText(htmlspecialchars(preg_replace("/[|]+/","",$asr[1])));
            }else{

                

                if(strstr($v,"{{7}}")){
                    $num=16;
                    $tuandui=DB::table("q_tb")->where(['quid'=>$quid,'type'=>1])->first();
                    $zhuban=DB::table("q_tb")->where(['quid'=>$quid,'type'=>2])->first();

                    if($tuandui){
                        $textrun->addText("■ 团队负责人介绍");
                        $textrun->addTextBreak();//换行符
            
                        $textrun->addImage($tuandui['img'],[
                            'width'=>450,
                            'align'=>'center'
                        ]);
                   }

                    if($zhuban){
                        $textrun->addTextBreak();//换行符

                        $textrun->addText("■ 主办律师介绍");
            
                        $textrun->addImage($zhuban['img'],[
                            'width'=>450,
                            'align'=>'center'
                        ]);

                    }

               
                    continue;

                }else if(strstr($v,"{{8")){
                    preg_match("~\{\{\s*(.*?)\s*\}\}~", $v, $arr);
                    $num++;
                    $textrun->addText(explode(",",$arr[1])[1],[
                        'color'=>"cc0e4b",
                        'size'=>14,
                        'bold'=>true
                    ]);
                }else if(strstr($v,"{{16")){
                    preg_match("~\{\{\s*(.*?)\s*\}\}~", $v, $arr);
                    $num=$num+ceil(strlen(explode(",",$arr[1])[1])/98);
                    $textrun->addText(explode(",",$arr[1])[1]);
                }else if(strstr($v,"{{9}}")){
                        if($num<=1){
                            $num=0;
                        }else if($num<=17){     
                            $num=0;
                            $section->addPageBreak();

                            $textrun= $section->createTextRun();
                        }else if($num==18){
                            $num=0;
                            $textrun= $section->createTextRun();
                        }else{
                            $num=0; 
                        }

                        

                        continue; 
                        //
                }else if(strstr($v,"{{14}}")){
                    $num=16;
                    $textrun->addImage("./ft/5.png",[
                        "width"=>450
                    ]);
                      

                }else if(strstr($v,"<noBold>")){
                    preg_match("~\<noBold\>\s*(.*?)\s*\</noBold\>~", $v, $arr);
                    $textrun->addText($arr[1]);
                    $num=$num+ceil(strlen($arr[1])/98);
                    // dd(ceil(strlen($arr[1])/98));

                }else if(strstr($v,"<jiacu>")){
                    preg_match("~\<jiacu\>\s*(.*?)\s*\</jiacu\>~", $v, $arr);
                    $textrun->addText("       ".$arr[1],['bold'=>true]);
                    $str=$arr[1];
                    $arr=explode("</jiacu>",$v);
                    $textrun->addText($arr[1]);
                    $str.=$arr[1];
                    $num=$num+ceil(strlen($str)/97);

//
                }
                else{
                    $num=$num+ceil(strlen("       ".$v)/98);
                    $textrun->addText("       ".htmlspecialchars(preg_replace("/[|]+/","",str_replace("      ","  ",$v))));
                   
                }

                // var_dump(count($newData),$k,$v,$num,strlen("       ".$v));
                if($num==19){
                    $num=0;
                    if($k+1==count($newData)){
                        continue;
                    }
                    $textrun= $section->createTextRun("myOwnStyle");
                    continue;
                }else if($num>19){
                    $num=$num-19;
                }

            }
            $textrun->addTextBreak();//换行符
        }



        $section3 = $phpWord->createSection();

                $section3->addImage("./ft/footer.jpg",[
                    'width'=>460,
                    'align'=>'center'
                ]);

        $timename=date("YmdHis",time());



        $this->get_pdf('./word/'.$timename.$wordName,$phpWord);

        return [
            'status_code'      => 200,
            'message'     =>  '查询成功',
            'data' => $timename.$wordName.'.pdf',
            ];
    }


    public function get_pdf($fileName,$phpWord){
        $domPdfPath = realpath("/home/www/jiali_api/vendor/dompdf/dompdf");
        \PhpOffice\PhpWord\Settings::setPdfRendererName("DomPDF");
        \PhpOffice\PhpWord\Settings::setPdfRendererPath($domPdfPath);
        $xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord , 'PDF');
        $xmlWriter->save($fileName.'.pdf');  
        var_dump($fileName.'.pdf');
    }
    


    public function get_yijian_content_chuli($quid){
        $content=DB::table("q_q_p_users")->where("id",$quid)->first()['content'];

        $content=  explode("\n",$content);  
    
        return [
            'status_code'      => 200,
            'message'     =>  '查询 成功',
            'data' => $content
            ];

    }


    public function select_user_questionnaire(Request $request){



        $data = $request->only("qid","status","uid");

        if(array_key_exists('qid',$data)&&!preg_match('/^[\d]{1,10}$/u',$data['qid'])) return ThrowApiExc('32012');
        if(!array_key_exists('status',$data)||!preg_match('/^[\d]{1,10}$/u',$data['status'])) return ThrowApiExc('42112');

        if(array_key_exists('qid',$data)&&if_authority($this->uid,"questionnaireDemo")){
            $qidArr = create(['questionnaireDemoIds']);
            if(!in_array($data['qid'],$qidArr['questionnaireDemoIds'])){
                return ThrowApiExc('32035');
            }
        }

        if(array_key_exists('uid',$data)){
            $uid=$data['uid'];
        }else{
            $uid=$this->uid;
        }

        if(array_key_exists('qid',$data)){
            $res = DB::table("Q_q_p_users")->where([
                ["qid",$data['qid']],
                ["userId",$uid],
                ["status",$data['status']]
            ])->orderBy("id","desc")->get()->toArray();
        }else{
            $res = DB::table("Q_q_p_users")->where([
                ["userId",$uid],
                ["status",$data['status']]
            ])->orderBy("id","desc")->get()->toArray();
        }



        $newres = second_array_unique_bykey($res,"history");



        foreach($newres as $k=>$v){
            $newres[$k]['historys']=[];
            foreach($res as $kk=>$vv){
                if($vv['id']!=$v['id']&&$vv['history']==$v['history']){
                    $newres[$k]['historys'][]=$vv;
                }
            }
        }

        foreach($newres as $k=>$v){
            $number = 0;
            foreach($v['historys'] as $kk=>$vv){
                $number=count($v['historys'])-$kk;
                $newres[$k]['historys'][$kk]['number']=$number;
            }
            $newres[$k]['number']=count($v['historys'])+1;
        }

        foreach($newres as $k=>$v){
            $consumingTime=0;
            $linshiTimes = DB::table("user_questionnaire_log")->where("quid",$v['id'])->get();
            foreach($linshiTimes as $Kk=>$vv){
                if($vv['created_time']&&$vv['stop_time']){
                    $consumingTime+=strtotime($vv['stop_time'])-strtotime($vv['created_time']);
                }
            }
            $newres[$k]['consumingTime']=$consumingTime;
        }

        if($res){
            return [
                'status_code'      => 200,
                'message'     =>  '查询成功',
                'data' => $newres
            ];
        }else{
            return [
                'status_code'      => 200,
                'message'     =>  '提交失败 联系后端开发',
                'data' => $res
            ];
        }

    }



    public function copy_user_questionnaire(Request $request,$quid){


            $data=$request->only("title");
            //title不存在 格式为空白字符 长度少于2大与20
            if(!array_key_exists('title',$data)||!preg_match('/^[\S\s]{2,20}$/u',$data['title'])) return ThrowApiExc('32001');
            $data2 = DB::table("Q_q_p_users")->where("id",$quid)->first();
            if($data2['history']==0){
                $countOldNum = DB::table("Q_q_p_users")->where("id",$quid)->orWhere("history",$quid)->count();
            }else{
                $countOldNum = DB::table("Q_q_p_users")->where("id",$quid)->orWhere("history",$quid)->count();
            }

            $data2['title']=$data['title'];
            $data2['signed']=0;
            $data2['agreement_number']=time().rand(1000,9999);
            $data2['createdTime']=date("Y-m-d h:i:s",time());
            $data2['UpdateTime']=date("Y-m-d h:i:s",time());


            //获取父问卷的历史版本ID
            $data2['history']= DB::table("Q_q_p_users")->where("id",$quid)->first()['history'];
            unset($data2['id']);

            $newquid = DB::table("Q_q_p_users")->insertGetId($data2);


            $arr = DB::insert("INSERT INTO Q_user_answer(userId,value,qpid,fornum,quid) SELECT userId,value,qpid,fornum,REPLACE(quid,{$quid},{$newquid}) from Q_user_answer where `quid`={$quid};");
            if($arr){
                return [
                    'status_code'      => 200,
                    'message'     =>  '复制成功',
                    'data' => $arr
                ];
            }else{
                return [
                    'status_code'      => 200,
                    'message'     =>  '提交失败 联系后端开发',
                    'data' => $arr
                ];
            }
    }

    public function get_list_user_data($quid){

        $questionaire = DB::table("Q_q_p_users")->where("id",$quid)->fitst();

        $data = DB::table("Q_q_p")->where(["questionnaire",$questionaire['qid'],['img',"!=",""]])->get();

        if($data){
            return [
                'status_code'      => 200,
                'message'     =>  '查询成功',
                'data' => $data
            ];
        }else{
            return [
                'status_code'      => 200,
                'message'     =>  '查询失败 联系后端开发',
                'data' => $data
            ];
        }

    }



    public function pdf_pdf(Request $request,$quid){
        get_pdf($quid);
    }



    public function cp(){


        $questionnaire=DB::table("q_questionnaire")->where([
            // ["id",">=","30"],
            // ['id',"<=","36"]
            // ["id",">=","37"],
            // ['id',"<=","45"]
            // ["id",">=","46"],
            // ['id',"<=","52"]
            // ["id",">=","53"],
            // ['id',"<=","59"]
            ['id',"=","31"]
        ])->get()->toArray();

        foreach($questionnaire as $k=>$v){
            //创建wid

            $wordData = DB::table("q_word")->where("id",$v['wid'])->first();
            unset($wordData['id']);
            $wordId = DB::table("q_word")->insertGetId($wordData);
            $this->cp_word($wordId,$v['wid']);
            //创建问卷
            $questionnaireData=$v;
            unset($questionnaireData['id']);
            $questionnaireData['wid']=$wordId;
            $questionnaireId = DB::table("q_questionnaire")->insertGetId($questionnaireData);
            $qpids = $this->cp_qqp($v['id'],$questionnaireId,0,0);
            $qofp=DB::table("q_relation")->whereIn("qpid",array_keys($qpids))->get()->toArray();
            foreach($qofp as $kk=>$vv){
                if($vv['qpid']) $qofp[$kk]['qpid']=$qpids[$vv['qpid']];
                if($vv['relationId']) $qofp[$kk]['relationId']=$qpids[$vv['relationId']];
                unset($qofp[$kk]['id']);
            }
            DB::table("q_relation")->insert($qofp);
        }

    }

    public function cp_qqp($oldQid,$newQid,$fid,$newfid){
        $newids=[];
        $data = DB::table("q_q_p")->where([
            ['questionnaireId',$oldQid],
            ['fqaspId',$fid]
        ])->get()->toArray();
        foreach($data as $k=>$v){
            $qpData=$v;
            unset($qpData['id']);
            $qpData['fqaspId']=$newfid;
            $qpData['questionnaireId']=$newQid;
            $newfid2=DB::table("q_q_p")->insertGetId($qpData);
            $newids[$v['id']]=$newfid2;

            $newids2=$this->cp_qqp($oldQid,$newQid,$v['id'],$newfid2);
            $newids=$newids+$newids2;
            
        }
        return $newids;
    }

    public function cp_word($newWordId,$oldWordId){
        $data = DB::table("q_word")->where("fWordId",$oldWordId)->get()->toArray();
        foreach($data as $k=>$v){
            $oldData = $v;
            $oldData['fWordId']=$newWordId;
            unset($oldData['id']);
            $wordId = DB::table("q_word")->insertGetId($oldData);
            $json_word = DB::table("q_word_json")->where("wid",$v['id'])->get()->toArray();
            foreach($json_word as $kk=>$vv){
                $word_json_data=$vv;
                unset($word_json_data['id']);
                $word_json_data['wid']=$wordId;
                DB::table("q_word_json")->insertGetId($word_json_data);
            }
            $this->cp_word($wordId,$v['id']);
        }
        return ;
    }



    public function demo2(){
        return aliyun_sms2(17611740979,'SMS_195586224','222222');
    }

    public function demo3(){
        return aliyun_huizhi(17611740979,'SMS_195586224','222222');
    }

}
